#include "StdAfx.h"
#include "DrawProgressBar.h"
namespace UILib
{
    CDrawProgressBar::CDrawProgressBar(void) : CDrawView( NULL, -1, NULL )\
        , pbmpBkg_( NULL )\
        , pbmpOver_(NULL)\
        , nTotal_(100)\
        , nCurPos_(0)\
    {
        memset( &pSplit_, 0, sizeof(SplitPos) );
    }

    CDrawProgressBar::CDrawProgressBar( Bitmap* pbmpBkg, Bitmap* pbmpOver, const PUIRect pRect, const PSplitPos pSplit /*= NULL*/, const UINT uID /*= -1\ */, const int nStatusCount /*= 4*/, ViewListener* pListener /*= NULL */ )\
        : CDrawView( pRect, uID, pListener )\
        , pbmpBkg_(pbmpBkg)\
        , pbmpOver_(pbmpOver)\
        , nTotal_(100)\
        , nCurPos_(0)\
    {
        if ( pSplit )
        {
            memcpy(&pSplit_, pSplit, sizeof(SplitPos) );
        }
        else
        {
            memset( &pSplit_, 0, sizeof(SplitPos) );
        }

    }
    CDrawProgressBar::~CDrawProgressBar( void )
    {
    }

    void CDrawProgressBar::DrawElement( const HDC hDC, const RECT &rcPaint )
    {
        CDrawView::bNeedInvalidate_ = false;
        if ( pbmpBkg_ == NULL || pbmpOver_ == NULL || !IsVisible() )
        {
            return;
        }
        int nW = pbmpBkg_->GetWidth();
        int nH = pbmpBkg_->GetHeight();

        RECT rcEmem = { 0 };
        CDrawView::GetRECT(rcEmem);

        Graphics graph(hDC);
        if ( pSplit_.lBegin == pSplit_.lEnd )
        {
            int nStatusW = nW;
            SIZE szElem = { rcEmem.right - rcEmem.left, rcEmem.bottom - rcEmem.top };
            Rect rcDest(rcEmem.left, rcEmem.top, szElem.cx, szElem.cy );
            graph.DrawImage(pbmpBkg_, rcDest, 0, 0, nW, nH, UnitPixel, NULL, NULL, NULL);
            {
                RECT rcClip = { rcEmem.left, rcEmem.top, rcEmem.right, rcEmem.bottom };
                int nFill = (szElem.cx * nCurPos_)/nTotal_;
                rcClip.right = rcClip.left + nFill;
                CClipRgn rgn(hDC, rcClip);
                graph.DrawImage(pbmpOver_, rcDest, 0, 0, nW, nH, UnitPixel, NULL, NULL, NULL);
            }
        }
        else
        {
            int nStatusW = nW;
            SIZE szElem = { rcEmem.right - rcEmem.left, rcEmem.bottom - rcEmem.top };
            Rect rcBkgDestL(rcEmem.left, rcEmem.top, pSplit_.lBegin, szElem.cy );
            Rect rcBkgDestM(rcEmem.left + pSplit_.lBegin, rcEmem.top, szElem.cx - ( nStatusW - (pSplit_.lEnd - pSplit_.lBegin) ), szElem.cy );
            Rect rcBkgDestR(rcEmem.right - ((nStatusW) - pSplit_.lEnd), rcEmem.top, (nStatusW) - pSplit_.lEnd, szElem.cy );

            graph.DrawImage(pbmpBkg_, rcBkgDestL, 0, 0, pSplit_.lBegin, nH, UnitPixel, NULL, NULL, NULL);
            graph.DrawImage(pbmpBkg_, rcBkgDestM, pSplit_.lBegin,0,  pSplit_.lEnd - pSplit_.lBegin, nH, UnitPixel, NULL, NULL, NULL);
            graph.DrawImage(pbmpBkg_, rcBkgDestR, pSplit_.lEnd, 0, nStatusW - pSplit_.lEnd, nH, UnitPixel, NULL, NULL, NULL);
        
            {
                RECT rcClip = { rcEmem.left, rcEmem.top, rcEmem.right, rcEmem.bottom };
                int nFill = (szElem.cx * nCurPos_)/nTotal_;
                rcClip.right = rcClip.left + nFill;
                CClipRgn rgn(hDC, rcClip);
                graph.DrawImage(pbmpOver_, rcBkgDestL, 0, 0, pSplit_.lBegin, nH, UnitPixel, NULL, NULL, NULL);
                graph.DrawImage(pbmpOver_, rcBkgDestM, pSplit_.lBegin,0,  pSplit_.lEnd - pSplit_.lBegin, nH, UnitPixel, NULL, NULL, NULL);
                graph.DrawImage(pbmpOver_, rcBkgDestR, pSplit_.lEnd, 0, nStatusW - pSplit_.lEnd, nH, UnitPixel, NULL, NULL, NULL);
            }
        }
    }

    void CDrawProgressBar::SetTotal( const int nTotal )
    {
        nTotal_ = nTotal;
    }

    void CDrawProgressBar::SetCurPos( const int nPos )
    {
        nCurPos_ = nPos;
        CDrawView::ForceInvalidate();
    }
}